<!--
 saveData() 保存数据的方法

------

 入参：
 flowId： 流程id
 nodeId： 节点id
 flowInstId： 流程实例id
 nodeInstId： 节点实例id
 isOperate:  操作类型  如果是insert，则会查询check item表，保存到 check item inst表中;
                       如果是update，则会查询check item inst表中，保存到 check item inst表中;
                       如果是不填或者其他，则会先去check item inst表中查询是否有对应节点实例的数据，如果有则返回，如果没有，则会去查询check item表，保存方法，会根据查询结果对应保存或者更新到check item inst表中;
-->
<template>
  <div style="position: relative;">
    <div class="tl-form-line-title" v-if="visible" style="display: block; float: left">
      检查项
    </div>
    <el-button icon="el-icon-document-checked" type="text" v-if="visible && !disableEditSubmit" style="position: absolute; top: 7px; right: 15px" @click="tempSave" :loading="saveLoading">暂存</el-button>
    <div style="padding-left:15px;" v-if="visible">
      <el-table ref="mainTable" class="tl-table" v-loading="loading" :data="datas" border fit>
        <el-table-column type="index" label="序号" width="55px" align="center"></el-table-column>
        <el-table-column prop="name" label="名称" width="150px" sortable="custom" sort-by="name" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="introduce" label="检查项内容" min-width="350px" sortable="custom" sort-by="introduce" align="left" header-align="center" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="检查结果" width="150px" sortable="custom" sort-by="introduce" align="center" header-align="center" :show-overflow-tooltip="true">
          <template slot-scope="scope">
            <el-select v-model="scope.row.result" filterable :disabled="disableEditSubmit">
              <el-option v-for="item in codeModel.flowCheckResultList" :key="item.code" :label="item.name" :value="item.code" />
            </el-select>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="备注" width="350px" align="left" header-align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.description" :disabled="disableEditSubmit"></el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
/** mixins */
import { mixin_list } from '@tapui/mixins/mixin_list'
import { mixin_approval } from '@tapui/mixins/mixin_approval'
/** 子组件 */
/** 工具类 */
import { getAction, putAction } from '@tapui/utils/http'
import { validateNull } from '@tapui/utils/validate'

/** VUE配置 */
export default {
  mixins: [mixin_list, mixin_approval],
  components: {},
  data() {
    return {
      moduleTitle: '检查项',
      urlPrefix: '/base/flowCheckItem/',
      url: {
        // 额外的url
        queryCheckItem: '/base/flowCheckItem/queryList',
        queryCheckCount: '/base/flowCheckItem/queryCount',
        queryInstCheckItem: '/base/flowCheckItemIns/queryList',
        queryCodeList: '/base/flowCheckItemIns/dataCodeList',
        add: '/base/flowCheckItemIns/add'
      },
      isQueryOnCreated: false,
      model: {},
      pageInfo: {
        currentPage: 1, // 当前页面
        pageSize: 0, // 每页记录数
        pageSizes: ['10', '50', '100'],
        totalCount: 0 // 总数
      },
      checkItemCount: 0,
      approvalNodeModel: {},
      disableEditSubmit: false,
      visible: false,
      tableHeight: 0,
      sourceType: '',
      flowid: 0,
      nodeid: 0,
      flowinsid: 0,
      nodeinsid: 0,
      saveLoading: false
    }
  },
  props: {
    render: {
      default: 'div',
      type: String
    },
    // 操作类型
    operate: {
      default: '',
      type: String
    }
  },
  methods: {
    close() {},
    open() {
      this.visible = false
      if (this.approvalNodeModel && this.approvalNodeModel.hasOwnProperty('nodetype')) {
        this.filtersInfo.nodeType = this.approvalNodeModel.nodetype
      }
      // 页面加载完毕执行
      if (this.flowid) {
        this.filtersInfo.flow_id = this.flowid
      }
      if (this.nodeid) {
        this.filtersInfo.node_id = this.nodeid
      }
      if (this.flowinsid) {
        this.filtersInfo.flow_inst_id = this.flowinsid
      }
      if (this.nodeinsid) {
        this.filtersInfo.node_inst_id = this.nodeinsid
      }
      if (this.operate) {
        this.filtersInfo.operate = this.operate
      }
      if (!this.disableEditSubmit) {
        this.url.list = this.url.queryCheckItem
        this.loadData()
      } else {
        this.url.list = this.url.queryInstCheckItem
        this.loadCount()
        this.loadData()
      }
    },
    loadCount() {
      // 查询条件
      var params = this.getQueryParams()
      getAction(this.url.queryCheckCount, params).then((res) => {
        if (res.success) {
          this.checkItemCount = res.result
          if (this.checkItemCount > 0) {
            this.visible = true
          }
        }
      })
    },
    resizeHandler() {},
    /** 列表数据加载开始 */
    loadData(paramMap) {
      if (!this.beforeLoadData()) return
      // 查询条件
      var params = this.getQueryParams()
      // 数据查询
      this.loading = true
      getAction(this.url.list, params)
        .then((res) => {
          if (res.success) {
            this.datas = res.result
          } else {
            this.$tap.alert(res.message, 'warning')
          }
          this.afterLoadData()
          this.loading = false
        })
        .catch((err) => {
          this.loading = false
        })
    },
    afterLoadData() {
      if (!this.datas || this.datas.length === 0) {
        return
      }
      this.visible = true
      this.datas.forEach((item) => {
        if (item.hasOwnProperty('checkname')) {
          item.name = item.checkname
        }
      })
    },
    asynSave(resolve, reject) {
      if (!this.visible) {
        resolve({ success: true })
        return
      }
      for (let i = 0; i < this.datas.length; i++) {
        if (!this.datas[i].hasOwnProperty('result') || this.datas[i].result === '') {
          reject('检查结果必填')
          return
        }
      }

      if (!this.flowinsid) {
        reject('流程实例id为空')
      }

      if (!this.nodeinsid) {
        reject('节点实例id为空')
      }
      this.datas.forEach((item) => {
        this.$set(item, 'flowid', this.flowid)
        this.$set(item, 'nodeid', this.nodeid)
        this.$set(item, 'flowinsid', this.flowinsid)
        this.$set(item, 'nodeinsid', this.nodeinsid)
        delete item.id
      })
      let paraMap = {}
      this.$set(paraMap, 'listData', this.datas)
      if (this.approvalNodeModel && this.approvalNodeModel.hasOwnProperty('nodetype')) {
        this.$set(paraMap, 'nodeType', this.approvalNodeModel.nodetype)
      }
      putAction(this.url.add, paraMap).then((res) => {
        resolve(res)
      })
    },
    tempSave() {
      if (!this.visible) {
        return
      }
      this.datas.forEach((item) => {
        this.$set(item, 'flowid', this.flowid)
        this.$set(item, 'nodeid', this.nodeid)
        this.$set(item, 'flowinsid', this.flowinsid)
        this.$set(item, 'nodeinsid', this.nodeinsid)
        delete item.id
      })
      let paraMap = {}
      this.$set(paraMap, 'listData', this.datas)
      if (this.approvalNodeModel && this.approvalNodeModel.hasOwnProperty('nodetype')) {
        this.$set(paraMap, 'nodeType', this.approvalNodeModel.nodetype)
      }
      this.loading = true
      putAction(this.url.add, paraMap).then((res) => {
        this.loading = false
        if (res.success) {
          this.$tap.message(res.message, 'success')
        } else {
          this.$tap.alert(res.message, 'warning')
        }
      })
    }
  },
  created() {
    this.visible = false
    this.checkItemCount = 0
  }
}
</script>
<style scoped></style>
